
/*	
This part is to generate the data for the composition exercise.

use "D:\Nanjing\2019\decomposition_data_file\data_file\combine_data\tmp_matlab200-05", clear
capture drop _Iproduct_c_* _Icic_adj_* _UNIT2_fe* _Iyear_*
*merge in rhos
merge m:m fup12 year using "D:\Nanjing\2019\decomposition_data_file\data_file\combine_data\rho2_31.dta"
drop _merge
append using "D:\Nanjing\2019\decomposition_data_file\data_file\combine_data\data_&_theta_NN==100-05.dta"

capture drop _Iproduct_c_* _Icic_adj_* _UNIT2_fe* _Iyear_*
replace exit_x     = 1 if n==1
replace rho_x      = 1 if rho_x==.
	

	*translog-no input correction coefficients
		gen bet2a2_l1	=	beta2_l
		gen bet2a2_m1	=	beta2_m
		gen bet2a2_k1	=	beta2_k
		gen bet2a2_l2	=	beta2_ll
		gen bet2a2_m2	=	beta2_mm
		gen bet2a2_k2	=	beta2_kk
		gen bet2a2_lm	=	beta2_lm
		gen bet2a2_lk	=	beta2_lk
		gen bet2a2_mk	=	beta2_mk
		gen bet2a2_le	=	beta2_le
		gen bet2a2_e2	=	beta2_ee
		gen bet2a2_e1	=	beta2_e
		gen bet2a2_me	=	beta2_me
		gen bet2a2_ke	=	beta2_ke
			drop beta2*
			rename bet2a2* beta2*

	
			g c_x           = rho_x
			g M_fjt_x       = c_x*exp(m)
			g L_fjt_x       = c_x*exp(l)
			g K_fjt_x       = c_x*exp(k)
			g E_fjt_x       = c_x*exp(e)
		

			g m_fjt_x       = ln(M_fjt_x)
			g l_fjt_x       = ln(L_fjt_x)
			g k_fjt_x       = ln(K_fjt_x)
            g e_fjt_x       = ln(E_fjt_x)
			

			*output elasticities for coefficients without input price correction
			g theta2_fjt_x   = beta2_m1 + 2*beta2_m2*m_fjt_x + beta2_lm*l_fjt_x + beta2_mk*k_fjt_x + beta2_me*e_fjt_x
			g theta2_L_fjt_x = beta2_l1 + 2*beta2_l2*l_fjt_x + beta2_lm*m_fjt_x + beta2_lk*k_fjt_x + beta2_le*e_fjt_x
			g theta2_K_fjt_x = beta2_k1 + 2*beta2_k2*k_fjt_x + beta2_lk*l_fjt_x + beta2_mk*m_fjt_x + beta2_ke*e_fjt_x
			g theta2_E_fjt_x = beta2_e1 + 2*beta2_e2*e_fjt_x + beta2_le*l_fjt_x + beta2_me*m_fjt_x + beta2_ke*e_fjt_x

			g RTS2_tl_x      = theta2_fjt_x + theta2_L_fjt_x + theta2_K_fjt_x +theta2_E_fjt_x
			

			label var theta2_fjt_x "materials output elasticity, no input correction"
			label var theta2_L_fjt_x "labor output elasticity, no input correction"
			label var theta2_K_fjt_x "capital output elasticity, no input correction"
			label var theta2_E_fjt_x "so2 emission elasticity, no input correction"

			

			g expenditure_m_fjt_x = c_x*totalintermediate_input
			g alpha_M_mpf_x = (expenditure_m_fjt_x)/ (c_x*totaloutput)
			g Markup_M_x    = theta2_fjt_x/(totalintermediate_input/totaloutput)
			
			
			
			egen ub=pctile(Markup_M_x),p(97)
			egen lb=pctile(Markup_M_x),p(3)
			drop if Markup_M_x>ub|Markup_M_x<lb
			bysort year fid: egen share_new=sum(c_x)
			gen c_x_new=c_x/share_new
			*replace Markup_M_x=1  if Markup_M_x<=0|Markup_M_x>5
  bysort year fid:  egen Markup_firm=total(c_x_new*Markup_M_x) 
			

***********************************************
* 1. firm-product level marginal costs
gen MC_fp=expenditure_m_fjt_x/(theta2_fjt_x*quantity) /*MC_fp: firm-product level marginal cost*/

* 2. emission tax
replace so2_emission=exp(e) /*e contains predicted emission when emission==0*/
bysort year fid: egen ems_new=sum(c_x_new*so2_emission/theta2_E_fjt_x)
gen tax_emi=totaloutput/(Markup_firm*ems_new)  /*firm-year level emission tax*/

* 3. firm-level elasticity of emission
bysort year fid: egen theta2_E_fjt_firm=sum(theta2_E_fjt_x*c_x_new) /*theta2_E_fjt_firm: firm level elasticity of emission*/

save  "D:\Nanjing\2019\decomposition_data_file\code for submission\Single_decomposition",replace
*/
*******************************************************************************************************





***********************************Decomposition : for single-tech***********************************************************************
*Part I: The single-technology decomposition



use "D:\Nanjing\2019\decomposition_final\data\Single_decomposition",clear
*gen tau=1/tax_emi /*the measure of emission policy*/

egen ub_e=pctile(theta2_E_fjt_firm),p(97)
egen lb_e=pctile(theta2_E_fjt_firm),p(3)
*keep if theta2_E_fjt_firm>lb_e|theta2_E_fjt_firm<ub_e
gen indc_y=.
replace indc_y=1 if year==2000
replace indc_y=2 if year==2005

keep if indc_y==1|indc_y==2
*1. generate the number of products each firm produce in each year
gen indc_prod=1
bysort fid indc_y: egen num_prod=sum(indc_prod) /*num_product number of product at firm-year level*/



*****************************************************************
* Define 1. continuing, 2. new entrants and 3. exit firms.
replace year=indc_y
gen party_id=fid

gen dummy1=.
replace dummy1=1 if year==1&(totaloutput!=.&totaloutput!=0)

gen dummy2=.
replace dummy2=1 if year==2&(totaloutput!=.&totaloutput!=0)

sort party_id
by party_id: egen max_d1=max(dummy1)
by party_id: egen max_d2=max(dummy2)

replace dummy1=max_d1 if dummy1==.
replace dummy2=max_d2 if dummy2==.

* (1)status=1: continuing firms
* (2)status=2: exit
* (3)status=3: entrant
gen status=.
replace status=1 if dummy1==1&dummy2==1
replace status=2 if dummy1==1&dummy2==.
replace status=3 if dummy1==.&dummy2==1

bysort party_id: gen n1=_n==1 if status==1
bysort party_id: gen n2=_n==1 if status==2
bysort party_id: gen n3=_n==1 if status==3
*------------------------------------------
*5-1(1): Compute the number of each group of firms
*------------------------------------------
egen N_con=sum(n1) /*number of continuing firms*/
egen N_ex=sum(n2)  /*number of exiters*/
egen N_en=sum(n3)  /*number of new entrants*/
gen N_total=(N_con*2+N_en+N_ex)/2 /*averge number of firms in 2000 and 2005*/
drop n1 n2 n3
**************************************************************************
*---------------------------------------------------
*5-1(2): compute delta_X_j for continuing firms:
*---------------------------------------------------
egen f_product=group(party_id product_code)
sort f_product MC_fp year
duplicates drop f_product year, force
tsset f_product year
tsfill, full
bysort f_product: egen product_code1=max(product_code)
bysort f_product: egen party_id1=max(party_id)
bysort f_product: egen status1=max(status)

bysort f_product: egen N_con1=max(N_con)
bysort f_product: egen N_en1=max(N_en)
bysort f_product: egen N_ex1=max(N_ex)
bysort f_product: egen N_total1=max(N_total)

replace product_code=product_code1
replace party_id=party_id1
replace status=status1
replace N_con=N_con1
replace N_en=N_en1
replace N_ex=N_ex1
replace N_total=N_total1

drop  product_code1 party_id1 status1 N_con1 N_en1 N_ex1 N_total1



sort f_product year
by f_product: gen quantity_l=l.quantity
*--------------------------------------------------------------------------------------------------------------------------
* for continuing firms, we focus on their continuing products
* for a given continuinf firm i, if a product j is NOT produced in both period, then we do not count this firm product pair.
*---------------------------------------------------------------------------------------------------------------------------
*replace quantity=0 if quantity==.& status==1  /*这里可能需要去掉！！！！！*/
*replace quantity_l=0 if quantity_l==. &status==1 &year==2

by f_product: gen diff_quant=quantity-quantity_l if status==1&year==2 /*delta_x_{ij} for continuing firms*/

bysort product_code year: egen diff_quant_s=mean(diff_quant)  if status==1/*delta_x_j for continuing firms*/
egen ub_quant=pctile(diff_quant_s),p(99)
egen lb_quant=pctile(diff_quant_s),p(1)
*replace diff_quant_s=. if diff_quant_s<ub_quant&diff_quant_s>lb_quant

*********************************************************************************************************

*---------------------------------------------------------
*5-1(3): compute delta_mc for continuing firm-product pair
*        compute delta_gamma for continuing firm-product pair
*----------------------------------------------------------
sort f_product year
by f_product: gen MC_fp_lag=l.MC_fp if status==1
by f_product: gen diff_mc=MC_fp-MC_fp_l if status==1&year==2 /*delta_mc_{ij} for continuing firm-product pair*/

by f_product: gen theta2_E_fjt_x_lag=l.theta2_E_fjt_x if status==1
by f_product: gen diff_gamma=theta2_E_fjt_x-theta2_E_fjt_x_lag /*delta_gamma_{ij} for continuing firm-product pair*/




*****************************************************************************************************
*-------------------------------------------------------
*5-1(4): compute mean(mc_{ij}) for continuing firm-product pair
*-------------------------------------------------------
* only generate average mc in the second year for continuing firms.
*replace MC_fp=. if MC_fp<0
gen indc_c=.
replace indc_c=1 if quantity!=.&status==1
bysort party_id product_code: egen mc_indc=sum(indc_c) if status==1
bysort party_id product_code: egen mc_con=mean(MC_fp) if status==1/*mc_{ij}: average margional cost for continuing firms */
replace mc_con=. if status==1&year==1
replace mc_con=. if status==1&mc_indc!=2

******************************************************************************

*-------------------------------------------------------
*5-1(5): compute bar_c{i}
*        compute bar_gamma
*        compute bar_tau_{i}
*        compute bar_emission
*        compute bar_(gamma_c): mean(gamma_i*c_i)
*        compute bar_x_{ij}
*        compute bar_rho: mean share
*        compute bar_gamma_fp: the average elasticity of emission at firm-product level 
*-------------------------------------------------------
bysort party_id: egen bar_c=sum(MC_fp*quantity/2) if status==1


bysort party_id year: egen theta2_E_fjt_firm_1=mean(theta2_E_fjt_firm) 
bysort party_id year: egen tax_emi_1=mean(tax_emi) 
bysort party_id year: egen so2_emission_1=mean(so2_emission) 
replace theta2_E_fjt_firm=theta2_E_fjt_firm_1
replace tax_emi=tax_emi_1
replace so2_emission=so2_emission_1
drop theta2_E_fjt_firm_1 tax_emi_1 so2_emission_1

bysort party_id: egen bar_gamma=mean(theta2_E_fjt_firm) if status==1
bysort party_id: egen bar_tau=mean(1/tax_emi) if status==1 
bysort party_id: egen bar_emission=mean(so2_emission) if status==1
bysort party_id: egen bar_gamma_c=sum(theta2_E_fjt_firm*MC_fp*quantity/2) if status==1


****************************************************************************************
* For each continuing firm, only keep one average value:
bysort party_id: gen n1=_n==1 if status==1
replace bar_gamma=bar_gamma*n1
replace bar_tau=bar_tau*n1
replace bar_emission=bar_emission*n1
replace bar_gamma_c=bar_gamma_c*n1

drop n1
* different than other bar_v, bar_x{ij}, bar_rho{ij} and bar_gamma_fp are at firm-product level.
gen bar_x=(quantity+quantity_l)/2 if status==1 /*for continuing firm-product pair bar_x>0, otherwise bar_x==.*/
capture drop share_new
bysort  party_id year: egen share_new=sum(c_x_new)
gen share_rho=c_x_new/share_new
sort f_product year
by f_product: gen share_rho_lag=l.share_rho
gen bar_rho=(share_rho+share_rho_lag)/2 if status==1 /*for continuing firm-product pair bar_rho>0, otherwise bar_rho==.*/

gen bar_gamma_fp=(theta2_E_fjt_x+theta2_E_fjt_x_lag)/2 if status==1 /*for continuing firm-product pair*/
************************************************************************************************************
*---------------------------------------------------------------------------------
*5-1(6): Group level average: group classification is by firms
*        compute bar_e_con1: average emission for continuing firms in year 2000
*        compute bar_e_con2: average emission for continuing firms in year 2005
*        compute bar_e_en: average emission for entrants in year 2005
*        compute bar_e_ex: average emission for exiters in year 2000
*-------------------------------------------------------------------------------------
bysort party_id year: gen indc_n=_n==1

egen bar_e_con1=sum(so2_emission*indc_n) if year==1&status==1
replace bar_e_con1=bar_e_con1/N_con  /*average emission for all continuing firms in year 2000*/
bysort year: egen indc_n2=max(bar_e_con1)
replace bar_e_con1=indc_n2       /*imput this value into each observation in 2000*/
drop indc_n2



egen bar_e_con2=sum(so2_emission*indc_n) if year==2&status==1
replace bar_e_con2=bar_e_con2/N_con /*average emission for all continuing firms in year 2005*/
bysort year: egen indc_n2=max(bar_e_con2)
replace bar_e_con2=indc_n2       /*input this value into each observation in 2005*/
drop indc_n2



egen bar_e_en=sum(so2_emission*indc_n) if year==2&status==3
replace bar_e_en=bar_e_en/N_en /*average emission for all entrants in year 2005*/
bysort year: egen indc_n2=max(bar_e_en)
replace bar_e_en=indc_n2       /*input this value into each observation in 2005*/
drop indc_n2


egen bar_e_ex=sum(so2_emission*indc_n) if year==1&status==2
replace bar_e_ex=bar_e_ex/N_ex /*average emission for all exiters in year 2000*/
bysort year: egen indc_n2=max(bar_e_ex)
replace bar_e_ex=indc_n2       /*input this value into each observation in 2000*/
drop indc_n2 indc_n


*--------------------------------------------------
*---------------------------------------------------------------------------------
*5-1(6)-2: Group level average: the group classification is by products: 
*        compute n_con: number of continuing products within continuing firms
*        compute n_en: number of new products within continuing firms
*        compute n_ex: number of exit products within continuing firms
*        compute bar_gamma_con1: average elasticity of all continuing firm-product pair in 2000;
*        compute bar_gamma_con2: average elasticity of all continuing firm-product pair in 2005;
*        compute bar_gamma_en: average elasticity of new products within continuing firms in 2005;
*        compute bar_gamma_ex: average elasticity of all exit products in year 2000;
*        compute bar_c_con1: average cost of contiuing firm-product pair in 2000
*        compute bar_c_con2: average cost of contiuing firm-product pair in 2005
*        compute bar_c_en: average cost of new products within continuing firms in 2000
*        compute bar_c_ex: average cost of exit products within continuing firms in 2005
*-------------------------------------------------------------------------------------
gen indc_n1=1 if quantity!=.&quantity_l!=.&status==1 /*indc_n1=1 for continuing firm-product pair*/
gen indc_n2=1 if quantity!=.&quantity_l==.&status==1&year==2 /*indc_n2=1 for new products within continuing firms*/
gen indc_n3=1 if quantity==.&quantity_l!=.&status==1 /*indc_n3=1 for exited products within continuing firms*/
bysort party_id: egen n_con=sum(indc_n1) if status==1 /*number of continuing firm-product pair within each continuing firm i*/
bysort party_id: egen n_en=sum(indc_n2) if status==1 /*number of new products within continuing firms*/
bysort party_id: egen n_ex=sum(indc_n3) if status==1 /*number of exit products withing continuing firms*/



bysort party_id product_code: egen indc_n=max(indc_n1)
replace indc_n1=indc_n
drop indc_n

bysort party_id product_code: egen indc_n=max(indc_n3)
replace indc_n3=indc_n
replace indc_n3=. if year==2


bysort party_id: egen bar_gamma_con1=sum(share_rho*theta2_E_fjt_x*indc_n1) if year==1
replace  bar_gamma_con1= bar_gamma_con1/n_con  /*average elasticity of all continuing firm-product pair in 2000;*/

bysort party_id: egen bar_c_con1=sum(quantity*MC_fp*indc_n1) if year==1
replace  bar_c_con1= bar_c_con1/n_con  /*average costs of all continuing firm-product pair in 2000;*/

*********************************************************************

bysort party_id: egen bar_gamma_con2=sum(share_rho*theta2_E_fjt_x*indc_n1) if year==2
replace  bar_gamma_con2= bar_gamma_con2/n_con  /*average elasticity of all continuing firm-product pair in 2005;*/

bysort party_id: egen bar_c_con2=sum(quantity*MC_fp*indc_n1) if year==2
replace  bar_c_con2= bar_c_con2/n_con  /*average costs of all continuing firm-product pair in 2005;*/
*************************************************************************


bysort party_id: egen bar_gamma_en=sum(share_rho*theta2_E_fjt_x*indc_n2) if year==2
replace  bar_gamma_en= bar_gamma_en/n_en  /*average elasticity of new products introduced by continuing firms in 2005;*/

bysort party_id: egen bar_c_en=sum(quantity*MC_fp*indc_n2) if year==2
replace  bar_c_en= bar_c_en/n_en  /*average costs of new products introduced by continuing firms in 2005;*/

**************************************************************************

bysort party_id: egen bar_gamma_ex=sum(share_rho*theta2_E_fjt_x*indc_n3) if year==1
replace  bar_gamma_ex= bar_gamma_ex/n_ex  /*average elasticity of exited products of continuing firms in 2000;*/
bysort party_id: egen bar_c_ex=sum(quantity*MC_fp*indc_n3) if year==1
replace  bar_c_ex= bar_c_ex/n_ex  /*average costs of new products introduced by continuing firms in 2005;*/

**************************************************************
* fill the value into each cell of continuing firm-product pair
bysort party_id product_code: egen bar_1=max(bar_gamma_con1)
bysort party_id product_code: egen bar_2=max(bar_gamma_con2)
bysort party_id product_code: egen bar_3=max(bar_gamma_en)
bysort party_id product_code: egen bar_4=max(bar_gamma_ex)
bysort party_id product_code: egen bar_5=max(bar_c_con1)
bysort party_id product_code: egen bar_6=max(bar_c_con2)
bysort party_id product_code: egen bar_7=max(bar_c_en)
bysort party_id product_code: egen bar_8=max(bar_c_ex)
replace bar_gamma_con1=bar_1
replace bar_gamma_con2=bar_2
replace bar_gamma_en=bar_3
replace bar_gamma_ex=bar_4
replace bar_c_con1=bar_5
replace bar_c_con2=bar_6
replace bar_c_en=bar_7
replace bar_c_ex=bar_8

drop bar_1 bar_2 bar_3 bar_4 bar_5 bar_6 bar_7 bar_8

drop indc_n indc_n1 indc_n2 indc_n3







/*
*-----------------------
egen bar_gamma_con1=sum(theta2_E_fjt_firm*indc_n) if year==1&status==1
replace bar_gamma_con1=bar_gamma_con1/N_con  /*average elasticity for all continuing firms in year 2000*/
bysort year: egen indc_n2=max(bar_gamma_con1)
replace bar_gamma_con1=indc_n2       /*imput this value into each observation in 2000*/
drop indc_n2


egen bar_gamma_con2=sum(theta2_E_fjt_firm*indc_n) if year==2&status==1
replace bar_gamma_con2=bar_gamma_con1/N_con  /*average elasticity for all continuing firms in year 2000*/
bysort year: egen indc_n2=max(bar_gamma_con2)
replace bar_gamma_con2=indc_n2       /*imput this value into each observation in 2000*/
drop indc_n2


egen bar_gamma_en=sum(theta2_E_fjt_firm*indc_n) if year==2&status==3
replace bar_gamma_en=bar_gamma_en/N_en  /*average elasticity for all entrants in year 2005*/
bysort year: egen indc_n2=max(bar_gamma_en)
replace bar_gamma_en=indc_n2       /*imput this value into each observation in 2005*/
drop indc_n2

egen bar_gamma_ex=sum(theta2_E_fjt_firm*indc_n) if year==1&status==2
replace bar_gamma_ex=bar_gamma_ex/N_ex  /*average elasticity for all exiters in year 2000*/
bysort year: egen indc_n2=max(bar_gamma_ex)
replace bar_gamma_ex=indc_n2       /*imput this value into each observation in 2000*/
drop indc_n2
*/



***************************************************************************************

*-------------------------------------------------------------------------
*5-1(7) compute delta_rho: the change of firm-product share
*-------------------------------------------------------------------------
gen diff_rho=share_rho-share_rho_l if status==1  /*for continuing firm-product pair share change*/

***************************************************************************************

*-------------------------------------------------------------------------
*5-1(8) compute delta_tau: the change of firm-level emission policy
*-------------------------------------------------------------------------


gen tau1=1/tax_emi if year==1& status==1 
bysort party_id: egen tau2=max(tau1) /*for each firm tau2 is the emission policy faced in period 1*/
gen delta_tau=1/tax_emi-tau2 
replace delta_tau=. if status==1&year==1 
bysort party_id: egen delta_tau1=mode(delta_tau)
replace delta_tau=delta_tau1
drop tau1 tau2 delta_tau1

***************************************************************************************

*-------------------------------------------------------------------------
*5-1(9) compute eta_e and ent_x: the number of entered and exit products
*-------------------------------------------------------------------------
gen prod_en=.
gen prod_ex=.
replace prod_en=1 if quantity!=.&quantity_l==.&year==2&status==1
replace prod_ex=1 if quantity==.&(quantity_l!=.)&status==1
bysort party_id: egen eta_e=sum(prod_en)
bysort party_id: egen eta_x=sum(prod_ex)
***************************************************************************************
/*
*We have the following constructed variable at hand:
(1). "bar_emission": average so2 emission for continuing firms in 2000 and 2005;
(2). "N_con", "N_en", "N_ex", and "N_total": number of continuing, entrant, exit and average number of firms.
(3). "bar_tau": the average of 1/tax for each continuing firm in 2000 and 2005;
(4). "bar_gamma": the average of elasticity to emission for each continuing firm in 2000 and 2005;
(5). "bar_c": the average total costs for each firm in 2000 and 2005;
(6). "bar_gamma_c": the avarege gamma*c_i for each firm in 2000 and 2005;
(7). "bar_rho": the average share of continuing firm-product pair in 2000 and 2005
(8). "bar_x": bar_x{ij}, the average quantity of x_ij at firm-product pair during 2000 - 2005;
(9). "bar_gamma_fp" the average elasticity of emission at firm-product level during 2000-2005;
(10). "bar_e_con1", "bar_e_con2": average emission for continuing group in 2000 and 2000, respectively;
(11). "bar_e_en" and "bar_e_ex": average emission for new entrants in year 2005 and for exiters in 2000, respectively.
(12). "bar_gamma_con1", "bar_gamma_con2": average elasticity for continuing firm-product pairs in 2000 and 2000, respectively;
(13). "bar_gamma_en" and "bar_gamma_ex": average elasticity for new  and for exit products within continuing firms, respectively.
(14). "bar_c_con1", "bar_c_con2": average costs for continuing firm-product pairs in 2000 and 2000, respectively;
(15). "bar_c_en" and "bar_c_ex": average costs for new and for exit products within continuing firms, respectively.
(14). "diff_quant": delta_x(ij) the quantity difference for continuing firm-product pair ij.
(16). "diff_quant_s": delta_x(j) the average quantity difference for all continuing firms.
(17). "diff_mc": the difference of firm-product level marginal cost between 2000 and 2005.
(18). "diff_rho": the difference of firm-product level share between 2000 and 2005.
(19). "diff_gamma": the difference of firm-product level emission elasticity between 2000 and 2005;
(20). "eta_e" and "eta_x": the number of entering and exit products for each firm. 
(21). "mc_con": average costs at firm-product level in 2000 and 2005; 
(22). "delta_tau": firm-level emission policy change during 2000-2005: tau_2005-tau_2000;
*/
***************************************************************************************
***************************************************************************************


*--------------------------------------------------------
* Step 2: Construct S_t (scale)
*-------------------------------------------------------- 
*egen ub_tau=pctile(bar_tau),p(99.9)
*egen lb_tau=pctile(bar_tau),p(0.1)
*replace bar_tau=. if bar_tau>=ub_tau|bar_tau<=lb_tau
gen comp1=bar_emission*(N_en-N_ex) /*No of firms*/

egen comp2=sum(bar_tau*bar_c*bar_gamma*(eta_e-eta_x)) 
replace comp2=N_total/N_con*comp2 /*adjusted number of products*/

gen aa=mc_con*diff_quant_s

egen min_aa=pctile(aa),p(1)
egen max_aa=pctile(aa),p(99)
replace aa=. if aa<=min_aa
replace aa=. if aa>=max_aa
bysort party_id: egen comp3= sum(aa) 

bysort party_id: egen comp31=max(comp3)

egen comp32=sum(bar_tau*bar_gamma*comp3)
replace comp3=N_total/N_con*comp32 /*adjusted average output growth*/


*Generate S_t
gen St=comp1+comp2+comp3

sum St comp1 comp2 comp3
egen s1=mean(comp1)
egen s2=mean(comp2)
egen s3=mean(comp3)

drop comp1 comp2 comp3

***********************************************************************

*------------------------------------------------------------b
* Step 3: Construct P_t (emission policy)
*------------------------------------------------------------


egen Pt=sum(bar_gamma_c*delta_tau) if status==1
replace Pt=N_total/N_con*Pt   



*****************************************************************************

*------------------------------------------------------------
* Step 4: Construct T_t (Technology)
*------------------------------------------------------------

bysort party_id: egen comp1=sum(bar_rho*diff_gamma)
egen comp12=sum(bar_tau*bar_c*comp1) if status==1
replace comp1=N_total/N_con*comp12 /*EBTC component*/

gen comp21=bar_x*diff_mc
egen max_comp21=pctile(comp21),p(97)
egen min_comp21=pctile(comp21),p(3)
replace comp21=. if comp21>=max_comp21|comp21<=min_comp21
bysort party_id: egen comp2=sum(comp21)
replace comp2=bar_tau*bar_gamma*comp2
egen max_comp2=pctile(comp2),p(99)
egen min_comp2=pctile(comp2),p(1)
replace comp2=. if comp2>=max_comp2|comp2<=min_comp2
egen comp22=sum(comp2)
replace comp2=N_total/N_con*comp22  /*Eifficiency component*/

gen Tt=comp1+comp2
sum comp1 comp2
egen t1=mean(comp1)
egen t2=mean(comp2)

drop comp1 comp12 comp2 comp22
***********************************************************************



*------------------------------------------------------------
* Step 5: Construct R_t (Reallocation)
*------------------------------------------------------------
bysort party_id: egen comp1=sum(mc_con*(diff_quant_s-diff_quant))
egen max_comp1=pctile(comp1),p(99)
egen min_comp1=pctile(comp1),p(1)


replace comp1=. if comp1>=max_comp1
replace comp1=. if comp1<=min_comp1

gen compw=bar_tau*bar_gamma*comp1
sum compw,d

egen ub_compw=pctile(compw),p(99)
egen lb_compw=pctile(compw),p(1)
replace compw=. if compw>=ub_compw|compw<=lb_compw
egen comp12=sum(compw) if status==1
replace comp1=N_total/N_con*comp12 /*Across firms component*/
sum comp1

bysort party_id: egen comp2=sum(bar_gamma_fp*diff_rho) if status==1
egen  comp22=sum(bar_tau*bar_c*comp2) if status==1
replace comp2=N_total/N_con*comp22 /*Across Products component*/

gen Rt=comp1+comp2


sum comp1 comp2
egen r1=mean(comp1)
egen r2=mean(comp2)

drop comp1 comp2 comp12


****************************************************************************************

 
*****************************************************************




*------------------------------------------------------------
* Step 6: Construct C_t (Churning)
*------------------------------------------------------------
gen comp11=N_en*(bar_e_en-bar_e_con2)
egen comp11_new=mode(comp11)
replace comp11=comp11_new
drop comp11_new
gen comp12=N_ex*(bar_e_con1-bar_e_ex)
egen comp12_new=mode(comp12)
replace comp12=comp12_new
drop comp12_new
gen comp1=comp11+comp12 /*Firm-level Churning*/


egen comp2=sum(bar_tau*bar_c*(n_en*(bar_gamma_en-bar_gamma_con2)+n_ex*(bar_gamma_con1-bar_gamma_ex))) if status==1
replace comp2=N_total/N_con*comp2   /*Product-level emissions churning*/


egen comp3=sum(bar_tau*bar_gamma*(n_en*(bar_c_en-bar_c_con2)+n_ex*(bar_c_con1-bar_c_ex))) if status==1
replace comp3=N_total/N_con*comp3 /*Product-level cost churning*/



gen Ct=comp1+comp2+comp3

sum comp1 comp2 comp3
egen c1=mean(comp1)
egen c2=mean(comp2)
egen c3=mean(comp3)
drop comp1 comp2 comp3



********************************************
gen Delta_E=St+Pt+Tt+Rt+Ct


*sum Delta_E St Pt Tt Rt Ct

egen a1=mode(St)
replace St=a1

egen a2=mode(Pt)
replace Pt=a2

egen a3=mode(Tt)
replace Tt=a3

egen a4=mode(Rt)
replace Rt=a4

egen a5=mode(Ct)
replace Ct=a5

egen a6=mode(Delta_E)
replace Delta_E=a6




******************************************Table 5: single-tech*****************************************************************

******************************************Table 5: single_tech*****************************************************************


******************************************Table 5: single_tech*****************************************************************



*---------------------------------------------------------
* Row 1 (columns 1-3) of Table 5 (for single-Technology)
*-----------------------------------------------------------
gen Scale=St/Delta_E*100
gen Policy=Pt/Delta_E*100
gen Technology=Tt/Delta_E*100
gen Reallocation=Rt/Delta_E*100
gen Churning=Ct/Delta_E*100


rename Reallocation Composition 
replace Composition=Composition+Churning 
replace Technology=Technology+Policy 

sum Scale Composition Technology Churning



********************Table 6 for single-tech*************************
********************Table 6 for single-tech*************************
*---------------------------------------------------------------
* The following code replicate the subcomponent share in Table 8
*-----------------------------------------------------------------
* (1) Subshare of Scale (row 1 and columns 2-4 of Table 6)
gen share_s1=s1/St*100
gen share_s2=s2/St*100
gen share_s3=s3/St*100

sum share_s1 share_s2 share_s3

**********************************************
* (2)subshare of Composition (row3 and columns 2-4  of Table 6)
gen Rt_new=Rt+Ct
gen share_r1=r1/Rt*100*(Rt/Rt_new)
gen share_r2=r2/Rt*100*(Rt/Rt_new)
gen share_r3=Ct/Rt*100*(Rt/Rt_new)

sum share_r1 share_r2 share_r3
*********************************************
* (3) subshare of Technology (row 5 and columns 2-4 of Table 6)

gen Tt_new=Tt+Pt
gen share_t1=t1/Tt*100*(Tt/Tt_new)
gen share_t2=t2/Tt*100*(Tt/Tt_new)
gen share_t3=Pt/Tt*100*(Tt/Tt_new)

sum share_t1 share_t2 share_t3




********************Table 7 for single-tech*************************
********************Table 7 for single-tech*************************
* (4)subshare of Churning
gen share_c1=c1/Ct*100
gen share_c2=c2/Ct*100
gen share_c3=c3/Ct*100


sum share_c1 share_c2 share_c3

**********************************************************************************************














***************************Multiple-technology Decomposition************************************************************
***************************Multiple-technology Decomposition************************************************************
***************************Multiple-technology Decomposition************************************************************


use "D:\Nanjing\2019\decomposition_final\data\multiple_decomposition",clear




gen MC_fp=expenditure_m_fjt_x/(theta2_fjt_x*quantity) /*MC_fp: firm-product level marginal cost*/

* 2. emission tax
replace so2_emission=exp(e) /*e contains predicted emission when emission==0*/
bysort year fid: egen ems_new=sum(c_x_new*so2_emission/theta2_E_fjt_x)
gen tax_emi=totaloutput/(Markup_firm*ems_new)  /*firm-year level emission tax*/

* 3. firm-level elasticity of emission
bysort year fid: egen theta2_E_fjt_firm=sum(theta2_E_fjt_x*c_x_new) /*theta2_E_fjt_firm: firm level elasticity of emission*/




*gen tau=1/tax_emi /*the measure of emission policy*/

egen ub_e=pctile(theta2_E_fjt_firm),p(97)
egen lb_e=pctile(theta2_E_fjt_firm),p(3)
*keep if theta2_E_fjt_firm>lb_e|theta2_E_fjt_firm<ub_e
gen indc_y=.
replace indc_y=1 if year==2000
replace indc_y=2 if year==2005

keep if indc_y==1|indc_y==2
*1. generate the number of products each firm produce in each year
gen indc_prod=1
bysort fid indc_y: egen num_prod=sum(indc_prod) /*num_product number of product at firm-year level*/



*****************************************************************
* Define 1. continuing, 2. new entrants and 3. exit firms.
replace year=indc_y
gen party_id=fid

gen dummy1=.
replace dummy1=1 if year==1&(totaloutput!=.&totaloutput!=0)

gen dummy2=.
replace dummy2=1 if year==2&(totaloutput!=.&totaloutput!=0)

sort party_id
by party_id: egen max_d1=max(dummy1)
by party_id: egen max_d2=max(dummy2)

replace dummy1=max_d1 if dummy1==.
replace dummy2=max_d2 if dummy2==.

* (1)status=1: continuing firms
* (2)status=2: exit
* (3)status=3: entrant
gen status=.
replace status=1 if dummy1==1&dummy2==1
replace status=2 if dummy1==1&dummy2==.
replace status=3 if dummy1==.&dummy2==1

bysort party_id: gen n1=_n==1 if status==1
bysort party_id: gen n2=_n==1 if status==2
bysort party_id: gen n3=_n==1 if status==3
*------------------------------------------
*5-1(1): Compute the number of each group of firms
*------------------------------------------
egen N_con=sum(n1) /*number of continuing firms*/
egen N_ex=sum(n2)  /*number of exiters*/
egen N_en=sum(n3)  /*number of new entrants*/
gen N_total=(N_con*2+N_en+N_ex)/2 /*averge number of firms in 2000 and 2005*/
drop n1 n2 n3
**************************************************************************
*---------------------------------------------------
*5-1(2): compute delta_X_j for continuing firms:
*---------------------------------------------------
egen f_product=group(party_id product_code)
sort f_product MC_fp year
duplicates drop f_product year, force
tsset f_product year
tsfill, full
bysort f_product: egen product_code1=max(product_code)
bysort f_product: egen party_id1=max(party_id)
bysort f_product: egen status1=max(status)

bysort f_product: egen N_con1=max(N_con)
bysort f_product: egen N_en1=max(N_en)
bysort f_product: egen N_ex1=max(N_ex)
bysort f_product: egen N_total1=max(N_total)

replace product_code=product_code1
replace party_id=party_id1
replace status=status1
replace N_con=N_con1
replace N_en=N_en1
replace N_ex=N_ex1
replace N_total=N_total1

drop  product_code1 party_id1 status1 N_con1 N_en1 N_ex1 N_total1



sort f_product year
by f_product: gen quantity_l=l.quantity
*--------------------------------------------------------------------------------------------------------------------------
* for continuing firms, we focus on their continuing products
* for a given continuinf firm i, if a product j is NOT produced in both period, then we do not count this firm product pair.
*---------------------------------------------------------------------------------------------------------------------------
*replace quantity=0 if quantity==.& status==1  /*这里可能需要去掉！！！！！*/
*replace quantity_l=0 if quantity_l==. &status==1 &year==2

by f_product: gen diff_quant=quantity-quantity_l if status==1&year==2 /*delta_x_{ij} for continuing firms*/

bysort product_code year: egen diff_quant_s=mean(diff_quant)  if status==1/*delta_x_j for continuing firms*/
egen ub_quant=pctile(diff_quant_s),p(99)
egen lb_quant=pctile(diff_quant_s),p(1)
*replace diff_quant_s=. if diff_quant_s<ub_quant&diff_quant_s>lb_quant

*********************************************************************************************************

*---------------------------------------------------------
*5-1(3): compute delta_mc for continuing firm-product pair
*        compute delta_gamma for continuing firm-product pair
*----------------------------------------------------------
sort f_product year
by f_product: gen MC_fp_lag=l.MC_fp if status==1
by f_product: gen diff_mc=MC_fp-MC_fp_l if status==1&year==2 /*delta_mc_{ij} for continuing firm-product pair*/

by f_product: gen theta2_E_fjt_x_lag=l.theta2_E_fjt_x if status==1
by f_product: gen diff_gamma=theta2_E_fjt_x-theta2_E_fjt_x_lag /*delta_gamma_{ij} for continuing firm-product pair*/




*****************************************************************************************************
*-------------------------------------------------------
*5-1(4): compute mean(mc_{ij}) for continuing firm-product pair
*-------------------------------------------------------
* only generate average mc in the second year for continuing firms.
*replace MC_fp=. if MC_fp<0
gen indc_c=.
replace indc_c=1 if quantity!=.&status==1
bysort party_id product_code: egen mc_indc=sum(indc_c) if status==1
bysort party_id product_code: egen mc_con=mean(MC_fp) if status==1/*mc_{ij}: average margional cost for continuing firms */
replace mc_con=. if status==1&year==1
replace mc_con=. if status==1&mc_indc!=2

******************************************************************************

*-------------------------------------------------------
*5-1(5): compute bar_c{i}
*        compute bar_gamma
*        compute bar_tau_{i}
*        compute bar_emission
*        compute bar_(gamma_c): mean(gamma_i*c_i)
*        compute bar_x_{ij}
*        compute bar_rho: mean share
*        compute bar_gamma_fp: the average elasticity of emission at firm-product level 
*-------------------------------------------------------
bysort party_id: egen bar_c=sum(MC_fp*quantity/2) if status==1


bysort party_id year: egen theta2_E_fjt_firm_1=mean(theta2_E_fjt_firm) 
bysort party_id year: egen tax_emi_1=mean(tax_emi) 
bysort party_id year: egen so2_emission_1=mean(so2_emission) 
replace theta2_E_fjt_firm=theta2_E_fjt_firm_1
replace tax_emi=tax_emi_1
replace so2_emission=so2_emission_1
drop theta2_E_fjt_firm_1 tax_emi_1 so2_emission_1

bysort party_id: egen bar_gamma=mean(theta2_E_fjt_firm) if status==1
bysort party_id: egen bar_tau=mean(1/tax_emi) if status==1 
bysort party_id: egen bar_emission=mean(so2_emission) if status==1
bysort party_id: egen bar_gamma_c=sum(theta2_E_fjt_firm*MC_fp*quantity/2) if status==1


****************************************************************************************
* For each continuing firm, only keep one average value:
bysort party_id: gen n1=_n==1 if status==1
replace bar_gamma=bar_gamma*n1
replace bar_tau=bar_tau*n1
replace bar_emission=bar_emission*n1
replace bar_gamma_c=bar_gamma_c*n1

drop n1
* different than other bar_v, bar_x{ij}, bar_rho{ij} and bar_gamma_fp are at firm-product level.
gen bar_x=(quantity+quantity_l)/2 if status==1 /*for continuing firm-product pair bar_x>0, otherwise bar_x==.*/
capture drop share_new
bysort  party_id year: egen share_new=sum(c_x_new)
gen share_rho=c_x_new/share_new
sort f_product year
by f_product: gen share_rho_lag=l.share_rho
gen bar_rho=(share_rho+share_rho_lag)/2 if status==1 /*for continuing firm-product pair bar_rho>0, otherwise bar_rho==.*/

gen bar_gamma_fp=(theta2_E_fjt_x+theta2_E_fjt_x_lag)/2 if status==1 /*for continuing firm-product pair*/
************************************************************************************************************
*---------------------------------------------------------------------------------
*5-1(6): Group level average: group classification is by firms
*        compute bar_e_con1: average emission for continuing firms in year 2000
*        compute bar_e_con2: average emission for continuing firms in year 2005
*        compute bar_e_en: average emission for entrants in year 2005
*        compute bar_e_ex: average emission for exiters in year 2000
*-------------------------------------------------------------------------------------
bysort party_id year: gen indc_n=_n==1

egen bar_e_con1=sum(so2_emission*indc_n) if year==1&status==1
replace bar_e_con1=bar_e_con1/N_con  /*average emission for all continuing firms in year 2000*/
bysort year: egen indc_n2=max(bar_e_con1)
replace bar_e_con1=indc_n2       /*imput this value into each observation in 2000*/
drop indc_n2



egen bar_e_con2=sum(so2_emission*indc_n) if year==2&status==1
replace bar_e_con2=bar_e_con2/N_con /*average emission for all continuing firms in year 2005*/
bysort year: egen indc_n2=max(bar_e_con2)
replace bar_e_con2=indc_n2       /*input this value into each observation in 2005*/
drop indc_n2



egen bar_e_en=sum(so2_emission*indc_n) if year==2&status==3
replace bar_e_en=bar_e_en/N_en /*average emission for all entrants in year 2005*/
bysort year: egen indc_n2=max(bar_e_en)
replace bar_e_en=indc_n2       /*input this value into each observation in 2005*/
drop indc_n2


egen bar_e_ex=sum(so2_emission*indc_n) if year==1&status==2
replace bar_e_ex=bar_e_ex/N_ex /*average emission for all exiters in year 2000*/
bysort year: egen indc_n2=max(bar_e_ex)
replace bar_e_ex=indc_n2       /*input this value into each observation in 2000*/
drop indc_n2 indc_n


*--------------------------------------------------
*---------------------------------------------------------------------------------
*5-1(6)-2: Group level average: the group classification is by products: 
*        compute n_con: number of continuing products within continuing firms
*        compute n_en: number of new products within continuing firms
*        compute n_ex: number of exit products within continuing firms
*        compute bar_gamma_con1: average elasticity of all continuing firm-product pair in 2000;
*        compute bar_gamma_con2: average elasticity of all continuing firm-product pair in 2005;
*        compute bar_gamma_en: average elasticity of new products within continuing firms in 2005;
*        compute bar_gamma_ex: average elasticity of all exit products in year 2000;
*        compute bar_c_con1: average cost of contiuing firm-product pair in 2000
*        compute bar_c_con2: average cost of contiuing firm-product pair in 2005
*        compute bar_c_en: average cost of new products within continuing firms in 2000
*        compute bar_c_ex: average cost of exit products within continuing firms in 2005
*-------------------------------------------------------------------------------------
gen indc_n1=1 if quantity!=.&quantity_l!=.&status==1 /*indc_n1=1 for continuing firm-product pair*/
gen indc_n2=1 if quantity!=.&quantity_l==.&status==1&year==2 /*indc_n2=1 for new products within continuing firms*/
gen indc_n3=1 if quantity==.&quantity_l!=.&status==1 /*indc_n3=1 for exited products within continuing firms*/
bysort party_id: egen n_con=sum(indc_n1) if status==1 /*number of continuing firm-product pair within each continuing firm i*/
bysort party_id: egen n_en=sum(indc_n2) if status==1 /*number of new products within continuing firms*/
bysort party_id: egen n_ex=sum(indc_n3) if status==1 /*number of exit products withing continuing firms*/



bysort party_id product_code: egen indc_n=max(indc_n1)
replace indc_n1=indc_n
drop indc_n

bysort party_id product_code: egen indc_n=max(indc_n3)
replace indc_n3=indc_n
replace indc_n3=. if year==2


bysort party_id: egen bar_gamma_con1=sum(share_rho*theta2_E_fjt_x*indc_n1) if year==1
replace  bar_gamma_con1= bar_gamma_con1/n_con  /*average elasticity of all continuing firm-product pair in 2000;*/

bysort party_id: egen bar_c_con1=sum(quantity*MC_fp*indc_n1) if year==1
replace  bar_c_con1= bar_c_con1/n_con  /*average costs of all continuing firm-product pair in 2000;*/

*********************************************************************

bysort party_id: egen bar_gamma_con2=sum(share_rho*theta2_E_fjt_x*indc_n1) if year==2
replace  bar_gamma_con2= bar_gamma_con2/n_con  /*average elasticity of all continuing firm-product pair in 2005;*/

bysort party_id: egen bar_c_con2=sum(quantity*MC_fp*indc_n1) if year==2
replace  bar_c_con2= bar_c_con2/n_con  /*average costs of all continuing firm-product pair in 2005;*/
*************************************************************************


bysort party_id: egen bar_gamma_en=sum(share_rho*theta2_E_fjt_x*indc_n2) if year==2
replace  bar_gamma_en= bar_gamma_en/n_en  /*average elasticity of new products introduced by continuing firms in 2005;*/

bysort party_id: egen bar_c_en=sum(quantity*MC_fp*indc_n2) if year==2
replace  bar_c_en= bar_c_en/n_en  /*average costs of new products introduced by continuing firms in 2005;*/

**************************************************************************

bysort party_id: egen bar_gamma_ex=sum(share_rho*theta2_E_fjt_x*indc_n3) if year==1
replace  bar_gamma_ex= bar_gamma_ex/n_ex  /*average elasticity of exited products of continuing firms in 2000;*/
bysort party_id: egen bar_c_ex=sum(quantity*MC_fp*indc_n3) if year==1
replace  bar_c_ex= bar_c_ex/n_ex  /*average costs of new products introduced by continuing firms in 2005;*/

**************************************************************
* fill the value into each cell of continuing firm-product pair
bysort party_id product_code: egen bar_1=max(bar_gamma_con1)
bysort party_id product_code: egen bar_2=max(bar_gamma_con2)
bysort party_id product_code: egen bar_3=max(bar_gamma_en)
bysort party_id product_code: egen bar_4=max(bar_gamma_ex)
bysort party_id product_code: egen bar_5=max(bar_c_con1)
bysort party_id product_code: egen bar_6=max(bar_c_con2)
bysort party_id product_code: egen bar_7=max(bar_c_en)
bysort party_id product_code: egen bar_8=max(bar_c_ex)
replace bar_gamma_con1=bar_1
replace bar_gamma_con2=bar_2
replace bar_gamma_en=bar_3
replace bar_gamma_ex=bar_4
replace bar_c_con1=bar_5
replace bar_c_con2=bar_6
replace bar_c_en=bar_7
replace bar_c_ex=bar_8

drop bar_1 bar_2 bar_3 bar_4 bar_5 bar_6 bar_7 bar_8

drop indc_n indc_n1 indc_n2 indc_n3







/*
*-----------------------
egen bar_gamma_con1=sum(theta2_E_fjt_firm*indc_n) if year==1&status==1
replace bar_gamma_con1=bar_gamma_con1/N_con  /*average elasticity for all continuing firms in year 2000*/
bysort year: egen indc_n2=max(bar_gamma_con1)
replace bar_gamma_con1=indc_n2       /*imput this value into each observation in 2000*/
drop indc_n2


egen bar_gamma_con2=sum(theta2_E_fjt_firm*indc_n) if year==2&status==1
replace bar_gamma_con2=bar_gamma_con1/N_con  /*average elasticity for all continuing firms in year 2000*/
bysort year: egen indc_n2=max(bar_gamma_con2)
replace bar_gamma_con2=indc_n2       /*imput this value into each observation in 2000*/
drop indc_n2


egen bar_gamma_en=sum(theta2_E_fjt_firm*indc_n) if year==2&status==3
replace bar_gamma_en=bar_gamma_en/N_en  /*average elasticity for all entrants in year 2005*/
bysort year: egen indc_n2=max(bar_gamma_en)
replace bar_gamma_en=indc_n2       /*imput this value into each observation in 2005*/
drop indc_n2

egen bar_gamma_ex=sum(theta2_E_fjt_firm*indc_n) if year==1&status==2
replace bar_gamma_ex=bar_gamma_ex/N_ex  /*average elasticity for all exiters in year 2000*/
bysort year: egen indc_n2=max(bar_gamma_ex)
replace bar_gamma_ex=indc_n2       /*imput this value into each observation in 2000*/
drop indc_n2
*/



***************************************************************************************

*-------------------------------------------------------------------------
*5-1(7) compute delta_rho: the change of firm-product share
*-------------------------------------------------------------------------
gen diff_rho=share_rho-share_rho_l if status==1  /*for continuing firm-product pair share change*/

***************************************************************************************

*-------------------------------------------------------------------------
*5-1(8) compute delta_tau: the change of firm-level emission policy
*-------------------------------------------------------------------------


gen tau1=1/tax_emi if year==1& status==1 
bysort party_id: egen tau2=max(tau1) /*for each firm tau2 is the emission policy faced in period 1*/
gen delta_tau=1/tax_emi-tau2 
replace delta_tau=. if status==1&year==1 
bysort party_id: egen delta_tau1=mode(delta_tau)
replace delta_tau=delta_tau1
drop tau1 tau2 delta_tau1

***************************************************************************************

*-------------------------------------------------------------------------
*5-1(9) compute eta_e and ent_x: the number of entered and exit products
*-------------------------------------------------------------------------
gen prod_en=.
gen prod_ex=.
replace prod_en=1 if quantity!=.&quantity_l==.&year==2&status==1
replace prod_ex=1 if quantity==.&(quantity_l!=.)&status==1
bysort party_id: egen eta_e=sum(prod_en)
bysort party_id: egen eta_x=sum(prod_ex)
***************************************************************************************
/*
*We have the following constructed variable at hand:
(1). "bar_emission": average so2 emission for continuing firms in 2000 and 2005;
(2). "N_con", "N_en", "N_ex", and "N_total": number of continuing, entrant, exit and average number of firms.
(3). "bar_tau": the average of 1/tax for each continuing firm in 2000 and 2005;
(4). "bar_gamma": the average of elasticity to emission for each continuing firm in 2000 and 2005;
(5). "bar_c": the average total costs for each firm in 2000 and 2005;
(6). "bar_gamma_c": the avarege gamma*c_i for each firm in 2000 and 2005;
(7). "bar_rho": the average share of continuing firm-product pair in 2000 and 2005
(8). "bar_x": bar_x{ij}, the average quantity of x_ij at firm-product pair during 2000 - 2005;
(9). "bar_gamma_fp" the average elasticity of emission at firm-product level during 2000-2005;
(10). "bar_e_con1", "bar_e_con2": average emission for continuing group in 2000 and 2000, respectively;
(11). "bar_e_en" and "bar_e_ex": average emission for new entrants in year 2005 and for exiters in 2000, respectively.
(12). "bar_gamma_con1", "bar_gamma_con2": average elasticity for continuing firm-product pairs in 2000 and 2000, respectively;
(13). "bar_gamma_en" and "bar_gamma_ex": average elasticity for new  and for exit products within continuing firms, respectively.
(14). "bar_c_con1", "bar_c_con2": average costs for continuing firm-product pairs in 2000 and 2000, respectively;
(15). "bar_c_en" and "bar_c_ex": average costs for new and for exit products within continuing firms, respectively.
(14). "diff_quant": delta_x(ij) the quantity difference for continuing firm-product pair ij.
(16). "diff_quant_s": delta_x(j) the average quantity difference for all continuing firms.
(17). "diff_mc": the difference of firm-product level marginal cost between 2000 and 2005.
(18). "diff_rho": the difference of firm-product level share between 2000 and 2005.
(19). "diff_gamma": the difference of firm-product level emission elasticity between 2000 and 2005;
(20). "eta_e" and "eta_x": the number of entering and exit products for each firm. 
(21). "mc_con": average costs at firm-product level in 2000 and 2005; 
(22). "delta_tau": firm-level emission policy change during 2000-2005: tau_2005-tau_2000;
*/
***************************************************************************************
***************************************************************************************

*--------------------------------------------------------
* Step 2: Construct S_t (scale)
*-------------------------------------------------------- 

gen comp1=bar_emission*(N_en-N_ex) /*No of firms*/


gen comp21=bar_tau*bar_c*bar_gamma
*egen max_comp21=max(comp21)
*replace comp21=. if comp21==max_comp21
gen comp22=comp21*(eta_e-eta_x)
replace comp22=. if comp22>=1e+9


egen comp2=sum(comp22) 
replace comp2=N_total/N_con*comp2 /*adjusted number of products*/

gen aa=mc_con*diff_quant_s

egen min_aa=pctile(aa),p(3)
egen max_aa=pctile(aa),p(97)
*replace aa=. if aa<=min_aa
*replace aa=. if aa>=max_aa
drop min_aa max_aa
bysort party_id: egen comp3= sum(aa) 

replace aa=bar_tau*bar_gamma*comp3

egen min_aa=pctile(aa),p(3)
egen max_aa=pctile(aa),p(97)
replace aa=. if aa<=min_aa
replace aa=. if aa>=max_aa

egen comp32=sum(aa)
replace comp3=N_total/N_con*comp32 /*adjusted average output growth*/

gen St=comp1+comp2+comp3

egen s1=mean(comp1)
egen s2=mean(comp2)
egen s3=mean(comp3)

sum St comp1 comp2 comp3
drop comp1 comp2 comp3 comp21 comp22
***********************************************************************

*------------------------------------------------------------b
* Step 3: Construct P_t (emission policy)
*------------------------------------------------------------

gen gamma_tau=bar_gamma_c*delta_tau
egen ub_gamma_tau=pctile(gamma_tau),p(99)
egen Pt=sum(gamma_tau) if status==1&gamma_tau<ub_gamma_tau
replace Pt=N_total/N_con*Pt   



*****************************************************************************

*------------------------------------------------------------
* Step 4: Construct T_t (Technology)
*------------------------------------------------------------

bysort party_id: egen comp1=sum(bar_rho*diff_gamma)
egen comp12=sum(bar_tau*bar_c*comp1) if status==1
replace comp1=N_total/N_con*comp12 /*EBTC component*/

gen comp21=bar_x*diff_mc
egen ub_comp21=pctile(comp21),p(97)
egen lb_comp21=pctile(comp21),p(3)
replace comp21=. if comp21>=ub_comp21|comp21<=lb_comp21
bysort party_id: egen comp2=sum(comp21)
replace comp2=bar_tau*bar_gamma*comp2
egen max_comp2=pctile(comp2),p(97)
egen min_comp2=pctile(comp2),p(3)
replace comp2=. if comp2>=max_comp2|comp2<=min_comp2
egen comp22=sum(comp2)
replace comp2=N_total/N_con*comp22  /*Eifficiency component*/

gen Tt=comp1+comp2
sum comp1 comp2
egen t1=mean(comp1)
egen t2=mean(comp2)
drop comp1 comp12 comp2 comp22 comp21


****************************************************************************************



*------------------------------------------------------------
* Step 5: Construct R_t (Reallocation)
*------------------------------------------------------------
*gen bar_c_product=(bar_c_con1+bar_c_con2)/2
bysort party_id: egen comp1=sum(mc_con*(diff_quant_s-diff_quant))
egen max_comp1=max(comp1)
egen min_comp1=min(comp1)


replace comp1=. if comp1==max_comp1
replace comp1=. if comp1==min_comp1

gen compw=bar_tau*bar_gamma*comp1
sum compw,d

egen ub_compw=pctile(compw),p(97)
egen lb_compw=pctile(compw),p(3)
replace compw=. if compw>=ub_compw|compw<=lb_compw
egen comp12=sum(compw) if status==1
replace comp1=N_total/N_con*comp12 /*Across firms component*/
sum comp1

bysort party_id: egen comp2=sum(bar_gamma_fp*diff_rho) if status==1
gen  comp22=bar_tau*bar_c*comp2 if status==1

egen min_comp22=pctile(comp22),p(0.5)
replace comp22=. if comp22<=min_comp22

egen comp23=sum(comp22)
replace comp2=N_total/N_con*comp23 /*Across Products component*/

gen Rt=comp1+comp2
sum comp1 comp2
egen r1=mean(comp1)
egen r2=mean(comp2)
drop comp1 comp12 comp2 comp22 comp23

 
*****************************************************************


*------------------------------------------------------------
* Step 6: Construct C_t (Churning)
*------------------------------------------------------------

gen comp11=N_en*(bar_e_en-bar_e_con2)
egen comp11_new=mode(comp11)
replace comp11=comp11_new
drop comp11_new
gen comp12=N_ex*(bar_e_con1-bar_e_ex)
egen comp12_new=mode(comp12)
replace comp12=comp12_new
drop comp12_new
gen comp1=comp11+comp12 /*Firm-level Churning*/


egen comp2=sum(bar_tau*bar_c*(n_en*(bar_gamma_en-bar_gamma_con2)+n_ex*(bar_gamma_con1-bar_gamma_ex))) if status==1
replace comp2=N_total/N_con*comp2   /*Product-level emissions churning*/


egen comp3=sum(bar_tau*bar_gamma*(n_en*(bar_c_en-bar_c_con2)+n_ex*(bar_c_con1-bar_c_ex))) if status==1
replace comp3=N_total/N_con*comp3 /*Product-level cost churning*/


gen Ct=comp1+comp2+comp3

sum Ct comp1 comp2 comp3
egen c1=mean(comp1)
egen c2=mean(comp2)
egen c3=mean(comp3)
drop comp1 comp2 comp3



********************************************
gen Delta_E=St+Pt+Tt+Rt+Ct


*sum Delta_E St Pt Tt Rt Ct

egen a1=mode(St)
replace St=a1

egen a2=mode(Pt)
replace Pt=a2

egen a3=mode(Tt)
replace Tt=a3

egen a4=mode(Rt)
replace Rt=a4

egen a5=mode(Ct)
replace Ct=a5

egen a6=mode(Delta_E)
replace Delta_E=a6



********************Table 5 for multi-tech*************************
********************Table 5 for multi-tech*************************
gen Scale=St/Delta_E*100
gen Policy=Pt/Delta_E*100
gen Technology=Tt/Delta_E*100
gen Reallocation=Rt/Delta_E*100
gen Churning=Ct/Delta_E*100


rename Reallocation Composition 
replace Composition=Composition+Churning 
replace Technology=Technology+Policy 

sum Scale Composition Technology Churning



********************Table 6 for multi-tech*************************
********************Table 6 for multi-tech*************************
*---------------------------------------------------------------
* The following code replicate the subcomponent share in Table 8
*-----------------------------------------------------------------
* (1) Subshare of Scale (row 2 and columns 2-4 of Table 6)
gen share_s1=s1/St*100
gen share_s2=s2/St*100
gen share_s3=s3/St*100

sum share_s1 share_s2 share_s3

**********************************************
* (2)subshare of Composition (row4 and columns 2-4  of Table 6)
gen Rt_new=Rt+Ct
gen share_r1=r1/Rt*100*(Rt/Rt_new)
gen share_r2=r2/Rt*100*(Rt/Rt_new)
gen share_r3=Ct/Rt*100*(Rt/Rt_new)

sum share_r1 share_r2 share_r3
*********************************************
* (3) subshare of Technology (row6 and columns 2-4 of Table 6)

gen Tt_new=Tt+Pt
gen share_t1=t1/Tt*100*(Tt/Tt_new)
gen share_t2=t2/Tt*100*(Tt/Tt_new)
gen share_t3=Pt/Tt*100*(Tt/Tt_new)

sum share_t1 share_t2 share_t3




********************Table 7 for multi-tech*************************
********************Table 7 for multi-tech*************************
* (4)subshare of Churning
gen share_c1=c1/Ct*100
gen share_c2=c2/Ct*100
gen share_c3=c3/Ct*100


sum share_c1 share_c2 share_c3

**********************************************************************************************






























